
import 'dart:async';

import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:yp/helper.dart';
import 'package:yp/services/api.dart';
import 'package:get/get.dart';

import 'state.dart';

class ResetPasswordLogic extends GetxController {
  final state = ResetPasswordState();

  @override
  void onInit() {
    super.onInit();

    state.phoneTextController = TextEditingController();
    state.phoneFocusNode = FocusNode();

    state.verificationCodeTextController = TextEditingController();
    state.verificationCodeFocusNode = FocusNode();

    state.passwordTextController = TextEditingController();
    state.passwordFocusNode = FocusNode();

    state.enterAgainPasswordTextController = TextEditingController();
    state.enterAgainPasswordFocusNode = FocusNode();

    state.newPassword = true;
    state.enterAgainPassword = true;
    state.whetherAvailable = false;
  }

  void newPasswordClock(bool password) {
    state.newPassword = password;
    update();
  }
  void enterAgainPasswordClick(bool enterAgain) {
    state.enterAgainPassword = enterAgain;
    update();
  }
  void whetherAvailableClick() {
    if (state.phoneTextController.text.length > 0 &&
        state.verificationCodeTextController.text.length > 0 &&
        state.passwordTextController.text.length > 0 &&
        state.enterAgainPasswordTextController.text.length > 0
    ) {
      state.whetherAvailable = true;
      update();
    } else {
      state.whetherAvailable = false;
      update();
    }
  }
  //请求发送验证码
  void sendSms(){
    var params = {
      "mobile":state.phoneTextController.text,
      "verify_code_type": "forgot"
    };
    Api.sendSms(params).listen((event) {
      if(event['code'] == 0){
        toast("验证码已发送,请注意查收");
        state.smsData = event;
      }else{
        toast(event['message']);
      }
      update();
    });

    state.timer = Timer.periodic(Duration(seconds: 1), (timer) {
      if (state.seconds == 0) {
        state.timer?.cancel();
        state.verifyStr = '重新发送';
        state.seconds = 60;
        update();
        return;
      }
      state.seconds = --state.seconds;
      state.verifyStr = "已发送${state.seconds}s";
      update();
    });
  }
  /// 提交
  void onSubmit() {
    String phone = state.phoneTextController.text;
    String code = state.verificationCodeTextController.text;
    String psw1 = state.passwordTextController.text;
    String psw2 = state.enterAgainPasswordTextController.text;
    if (phone.isEmpty) {
      toast("请输入手机号");
      return;
    }
    if (!RegexUtil.isMobileSimple(phone)) {
      toast("手机号格式错误");
      return;
    }
    if (code.isEmpty) {
      toast("请输入验证码");
      return;
    }
    if (psw1.isEmpty) {
      toast("请输入新密码");
      return;
    }
    if (psw2.isEmpty) {
      toast("请再次输入新密码");
      return;
    }
    if (psw1 != psw2) {
      toast("两次输入的密码不一致");
      return;
    }

    var params = {
      "mobile": phone,
      "sms_verify_code": code,
      "sms_verify_type":"forgot",
      "sms_verify_token": state.smsData['token'],
      "password": psw1,
      "confirm_password": psw2,
      'use_platform': 20,
    };

    print(params);
    Api.resetPassword(params).listen((event) {
      if (event['code'] == 0) {
        toast("密码重置成功");
        Get.back();
      } else {
        toast(event['message']);
      }
    });
  }

  @override
  void onClose() {
    super.onClose();
    state.timer?.cancel();
  }

}